package cn.tedu.sims.controller;

import cn.tedu.sims.pojo.dto.UserLoginByEmailDTO;
import cn.tedu.sims.pojo.dto.UserLoginByPhoneDTO;
import cn.tedu.sims.pojo.dto.UserLoginDTO;
import cn.tedu.sims.pojo.dto.UserRegDTO;
import cn.tedu.sims.pojo.vo.LocationListItemVO;
import cn.tedu.sims.pojo.vo.UserLoginByUserNameAndRoleVO;
import cn.tedu.sims.service.UserService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户管理")
@Validated
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 20)
    @PostMapping("/add-new")
    public JsonResult<Void> AddNew(@Valid UserRegDTO userRegDTO){
        System.out.println("userRegDTO = " + userRegDTO);
        userService.insert(userRegDTO);
        return JsonResult.ok();
    }

    @ApiOperation("登陆")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> Login(@Valid UserLoginDTO userLoginDTO){
        System.out.println("userLoginDTO = " + userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("手机号码登陆")
    @ApiOperationSupport(order = 51)
    @PostMapping("/login/phone")
    public JsonResult<String> LoginByPhone(@Valid UserLoginByPhoneDTO userLoginByPhoneDTO){
        System.out.println("userLoginByPhoneDTO = " + userLoginByPhoneDTO);
        String jwt = userService.loginByPhone(userLoginByPhoneDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("邮箱登陆")
    @ApiOperationSupport(order = 52)
    @PostMapping("/login/email")
    public JsonResult<String> LoginByEmail(@Valid UserLoginByEmailDTO userLoginByEmailDTO){
        log.debug("正在处理【邮箱登录】的请求,参数:{}",userLoginByEmailDTO);
        String jwt=userService.loginByEmail(userLoginByEmailDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("发送邮箱")
    @ApiOperationSupport(order = 100)
    @GetMapping("/login/email/send/{email}")
    public JsonResult<Void> send(@PathVariable String email){
        log.debug("正在处理【发送邮箱】的请求,参数:{}",email);
        System.out.println("email = " + email);
        userService.sendEmail(email);
        return JsonResult.ok();
    }

    @ApiOperation("发送手机号验证码")
    @ApiOperationSupport(order = 105)
    @GetMapping("/login/phone/send/{phone}")
    public JsonResult<Void> sendByPhone(@PathVariable Long phone){
        System.out.println("phone = " + phone);
        userService.senPhone(phone);
        return JsonResult.ok();
    }


    @ApiOperation("通过手机号查询")
    @ApiOperationSupport(order = 400)
    @GetMapping("/selectByPhone/{phone}")
    public JsonResult<UserLoginByUserNameAndRoleVO> selectByPhone(@PathVariable Long phone){
        UserLoginByUserNameAndRoleVO user= userService.selectByPhone(phone);
        return JsonResult.ok(user);
    }

    @ApiOperation("通过邮箱查询")
    @ApiOperationSupport(order = 401)
    @GetMapping("/selectByEmail/{email}")
    public JsonResult<UserLoginByUserNameAndRoleVO> selectByEmail(@PathVariable String email){
        UserLoginByUserNameAndRoleVO user= userService.selectByEmail(email);
        return JsonResult.ok(user);
    }

    @ApiOperation("通过用户名查询")
    @ApiOperationSupport(order = 402)
    @GetMapping("/selectByUserName/{username}")
    public JsonResult<UserLoginByUserNameAndRoleVO> selectByUserName(@PathVariable String username){
        UserLoginByUserNameAndRoleVO user= userService.selectByUserName(username);
        return JsonResult.ok(user);
    }

    @ApiOperation("查询地区")
    @ApiOperationSupport(order = 402)
    @GetMapping("/select/region")
    public JsonResult<List<LocationListItemVO>> list(){
        List<LocationListItemVO> list=userService.selectByRegion();
        return JsonResult.ok(list);
    }
}
